package com.mhkj.revise.monthPlan.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.joda.time.DateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.revise.monthPlan.entity.Follow;
import com.mhkj.revise.monthPlan.repository.PlanMonthAddRepository;
import com.mhkj.revise.monthPlan.service.PlanMonthAddService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.util.DateUtils;
import com.mics.core.common.util.UserHelper;
import com.mics.core.system.base.service.impl.BaseServiceImpl;

@Service
public class PlanMonthAddServiceImpl extends BaseServiceImpl<Follow> implements PlanMonthAddService {
	@Resource
	private PlanMonthAddRepository planMonthAddRepository;

	@Resource
	public void setPlanMonthAddRepository(PlanMonthAddRepository planMonthAddRepository) {
		super.setBaseRepository(planMonthAddRepository);
	}

	@Override
	public List<Follow> getByEmployeeId(String employeeId) {
		return planMonthAddRepository.getByEmployeeId(employeeId);
	}

	@Override
	public Follow getByUserAndPlanId(String userId, long planNodeId) {
		return planMonthAddRepository.getByUserAndPlanId(userId,planNodeId);
	}

	@Override


	public Pagination<Follow> listByPageinfo(Follow follow,String nodeName, String yearMonth,String state,String startDate,String endDate,   int pageIndex, int pageSize) {

		Specification<Follow> spec = new Specification<Follow>() {
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<Follow> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Join<Object, Object> plan = root.join("planNode").join("plan", JoinType.INNER);
				Join<Object, Object> planType = plan.join("planType",JoinType.INNER);
				//失控
				if(state.equals("23")) {
					Join<Object, Object> planType1 = root.join("planNode",JoinType.INNER);
					Predicate p = criteriaBuilder.equal(planType1.get("riskState").as(Integer.class), 2);
					/*Predicate p1 = criteriaBuilder.notEqual(planType1.get("state").as(Integer.class), 2);
					lstPredicates.add(p1);*/
					lstPredicates.add(p);;
					//逾期
				}else if(state.equals("7")){
					Join<Object, Object> planType1 = root.join("planNode",JoinType.INNER);
					Predicate p1 = criteriaBuilder.equal(planType1.get("state").as(Integer.class), 7);
					Predicate p2 = criteriaBuilder.notEqual(planType1.get("riskState").as(Integer.class), 2);
					lstPredicates.add(p2);
					lstPredicates.add(p1);
				}
				//风险
				else if(state.equals("1")){
					Join<Object, Object> planType1 = root.join("planNode",JoinType.INNER);
					Predicate p2 = criteriaBuilder.equal(planType1.get("riskState").as(Integer.class), 1);
					Predicate p1 = criteriaBuilder.notEqual(planType1.get("state").as(Integer.class), 0);
					lstPredicates.add(p1);
					lstPredicates.add(p2);
					//运行中
				}	else if(state.equals("2")){
					Join<Object, Object> planType1 = root.join("planNode",JoinType.INNER);
					Predicate p3 = criteriaBuilder.equal(planType1.get("state").as(Integer.class), 2);
					Predicate p2 = criteriaBuilder.equal(planType1.get("riskState").as(Integer.class), 0);
					lstPredicates.add(p3);
					lstPredicates.add(p2);
				}

				else{
					Predicate p3 = criteriaBuilder.equal(plan.get("state").as(Integer.class), 2);
					lstPredicates.add(p3);
				}
				if (StringUtils.hasText(nodeName)) {
					Join<Object, Object> planType1 = root.join("planNode",JoinType.INNER);
 					Predicate tp = criteriaBuilder.like(planType1.get("nodeName").as(String.class), "%" + nodeName + "%");
					lstPredicates.add(tp);
				}
				if (!StringUtils.isEmpty(startDate)) {
					Join<Object, Object> planType1 = root.join("planNode",JoinType.INNER);
					try {
						Date endDate1 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
						Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
						Predicate p4 = criteriaBuilder.lessThanOrEqualTo(planType1.get("beginDate"), endDate1);
						Predicate p5 = criteriaBuilder.greaterThanOrEqualTo(planType1.get("endDate"), beginDate);
						lstPredicates.add(p4);
						lstPredicates.add(p5);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}


				String employeeId = UserHelper.getCurrentUserId();
				lstPredicates.add(criteriaBuilder.equal(root.get("createBy").as(String.class), employeeId));


				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<Follow> pageT = planMonthAddRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
		Pagination<Follow> pagination = new Pagination<Follow>();
		pagination.setTotal(planMonthAddRepository.count(spec));
		List<Follow> follows = pageT.getContent();
		pagination.setData(follows);
		return pagination;
	}

	@Override
	public List<Follow> getplanNodeId(long planNodeId,String employeeId) {
		return planMonthAddRepository.getByplanNodeId(planNodeId,employeeId);
	}
}
